<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Progressive Play Server
 * User Controller
 * /controllers/user.php
 * By: Yukwunhang
 */
class User extends MY_Controller
{
	// Constructor
	function User()
	{
		parent::MY_Controller();
		// Load models/libraries/helpers
		$this->load->model('player_model', 'player');
	}
	
	/**
	 * Login the user with Fracs V2 Auth library
	 * @return 
	 */
	function login()
	{
		// XSS cleaning
		$_POST = $this->input->xss_clean($_POST);
		
		// Login with Fracs auth class.
		$result = $this->auth->login();
		if(!$result)
		{
			// Set login_fail to TRUE in order to display error message.
			$this->session->set_flashdata('login_fail', TRUE);
		}
		else
		{
			// Add user to the database if the record does not exist.
			$this->player->add_player_if_not_exist($this->auth->dbase->convert2_fid($_POST['user']));
			
			// Success; update IP if changed
			$changed = $this->player->update_ip_if_changed($this->auth->dbase->convert2_fid($_POST['user']));
			if ($changed)
			{
				// Set update_ip flashdata to TRUE; only if changed.
				$this->session->set_flashdata('update_ip', TRUE);
			}
		}
		CI_redirect();
	}
	
	/**
	 * Logout the user with Fracs V2 Auth library
	 * @return 
	 */
	function logout()
	{
		$this->auth->logout();
		// Set logout_success to TRUE
		$this->session->set_flashdata('logout_success', TRUE);
		
		CI_redirect();
	}
	
	/**
	 * Update IP
	 */
	function update_ip()
	{
		//Update ip if changed
		$this->player->update_ip_if_changed($this->auth->id());
		
		// Set update_ip flashdata to TRUE, regardless if the user's IP is changed or not.
		// Feedback to the user is needed when they click a link.
		$this->session->set_flashdata('update_ip', TRUE);
		CI_redirect();
	}
}
/*
 * End of file
 */